package tools.vetorial;

import java.awt.Point;
import java.awt.geom.Line2D;

import core.DrawContext;
import core.Lines;
import core.Object2D;
import core.OurGraphics;
import core.Point3D;
import core.VectorialObject;

public class VectorialLineTool extends PointSelectionTool {

	private Lines object;
	
	private VectorialLineProxy proxy = new VectorialLineProxy();
	
	private Point proxyPoint = new Point();
	
	private int currentPoint = 0;

	@Override
	public void addPoint(int x, int y, int order, DrawContext ctx) {
		object.addVertice(new Point3D(x, y));
		
		if(currentPoint-1 >= 0) {
			object.join(object.getVertice(currentPoint-1),
						object.getVertice(currentPoint), getColor());
		}
		
		currentPoint++;
	}

	@Override
	public void updateProxy(int x, int y, DrawContext ctx) {
		proxyPoint.x = x;
		proxyPoint.y = y;
	}

	@Override
	public void pencilUp(DrawContext ctx) {
		if(object.getVerticeCount() > 1) {
			ctx.addObject2d(object);
			ctx.setChanged(true);
		}
	}
	
	@Override
	public void pencilDown(DrawContext ctx) {
		object = new Lines();
		currentPoint = 0;
	}

	@Override
	public Object2D getProxy() {
		return proxy;
	}
	
	private class VectorialLineProxy implements Object2D  {
		
		private Line2D.Double line = new Line2D.Double();
		
		@Override
		public void draw(OurGraphics g2) {
			g2.setColor(getColor());
			
			object.draw(g2);
			
			if(object.getVerticeCount() > 0 && proxyPoint != null){
				
				Point3D lastVertice = object.getVertice(object.getVerticeCount()-1);
				
				line.x1 = lastVertice.getX();
				line.y1 = lastVertice.getY();
				line.x2 = proxyPoint.x;
				line.y2 = proxyPoint.y;
						
				g2.drawLine((int)line.x1, (int)line.y1, (int)line.x2, (int)line.y2);
			}
		}
	}

	@Override
	public void onSelection(VectorialObject vo) {
		// TODO Auto-generated method stub
		
	}

}
